package com.xy.bioapi.handle;

import com.xy.bioapi.entity.Result;
import com.xy.bioapi.enums.ResultEnum;
import com.xy.bioapi.exception.TestException;
import com.xy.bioapi.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常捕获
 * Created by caeyer on 2017/7/12.
 */

@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handleTest(Exception e){
        if (e instanceof TestException){
            TestException testException = (TestException) e;
            return ResultUtil.error(testException.getInvoke_code(),testException.getMessage());
        }else{
            //系统级异常
            //logger.info("[SYSTEM ERROR]={}",e);
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKNOW_ERROR);
        }
    }
}
